package com.kumi.shop.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.kumi.shop.product.bean.Attr;
import com.kumi.shop.product.bean.AttrAttrgroupRelation;
import com.kumi.shop.product.service.AttrAttrgroupRelationService;
import com.kumi.shop.product.service.AttrService;
import com.kumi.shop.product.service.CategoryService;
import com.kumi.shop.product.vo.AttrGroupRespVo;
import com.kumi.shop.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kumi.shop.product.bean.AttrGroup;
import com.kumi.shop.product.service.AttrGroupService;
import com.kumi.common.utils.PageUtils;
import com.kumi.common.utils.R;


/**
 * 属性分组
 *
 * @author æ­å°
 * @email 1971240051@qq.com
 * @date 2021-12-30 15:40:51
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService service;

    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }

    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrGroupRespVo> voList){
        service.saveBatch(voList);
        return R.ok();
    }

    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<Attr> attrList = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",attrList);
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoAttrRelation(@PathVariable("attrgroupId") Long attrgroupId,
                                @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") long catelogId) {

//        PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroup attrGroup = attrGroupService.getById(attrGroupId);
        Long attrlogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCateLogPath(attrlogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroup attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroup attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


    @PostMapping("attr/relation/delete")
    public R deleteRale(@RequestBody AttrGroupRespVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }
}
